package com.yic.module.qms.controller.admin.inspectschemeitem;

import cn.hutool.core.util.ObjectUtil;
import com.yic.module.base.api.item.ItemApi;
import com.yic.module.base.api.item.dto.ItemRespDTO;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.yic.framework.common.pojo.PageResult;
import com.yic.framework.common.pojo.CommonResult;
import static com.yic.framework.common.pojo.CommonResult.success;

import com.yic.framework.excel.core.util.ExcelUtils;

import com.yic.framework.operatelog.core.annotations.OperateLog;
import static com.yic.framework.operatelog.core.enums.OperateTypeEnum.*;

import com.yic.module.qms.controller.admin.inspectschemeitem.vo.*;
import com.yic.module.qms.dal.dataobject.inspectschemeitem.InspectSchemeItemDO;
import com.yic.module.qms.convert.inspectschemeitem.InspectSchemeItemConvert;
import com.yic.module.qms.service.inspectschemeitem.InspectSchemeItemService;

@Tag(name = "管理后台 - 检验方案产品关联")
@RestController
@RequestMapping("/qms/inspect-scheme-item")
@Validated
public class InspectSchemeItemController {

    @Resource
    private InspectSchemeItemService inspectSchemeItemService;
    @Resource
    private ItemApi itemApi;

    @PostMapping("/create")
    @Operation(summary = "创建检验方案产品关联")
    @PreAuthorize("@ss.hasPermission('qms:inspect-scheme-item:create')")
    public CommonResult<Long> createInspectSchemeItem(@Valid @RequestBody InspectSchemeItemCreateReqVO createReqVO) {
        return success(inspectSchemeItemService.createInspectSchemeItem(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新检验方案产品关联")
    @PreAuthorize("@ss.hasPermission('qms:inspect-scheme-item:update')")
    public CommonResult<Boolean> updateInspectSchemeItem(@Valid @RequestBody InspectSchemeItemUpdateReqVO updateReqVO) {
        inspectSchemeItemService.updateInspectSchemeItem(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除检验方案产品关联")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('qms:inspect-scheme-item:delete')")
    public CommonResult<Boolean> deleteInspectSchemeItem(@RequestParam("id") Long id) {
        inspectSchemeItemService.deleteInspectSchemeItem(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得检验方案产品关联")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('qms:inspect-scheme-item:query')")
    public CommonResult<InspectSchemeItemRespVO> getInspectSchemeItem(@RequestParam("id") Long id) {
        InspectSchemeItemDO inspectSchemeItem = inspectSchemeItemService.getInspectSchemeItem(id);
        return success(InspectSchemeItemConvert.INSTANCE.convert(inspectSchemeItem));
    }

    @GetMapping("/list")
    @Operation(summary = "获得检验方案产品关联列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('qms:inspect-scheme-item:query')")
    public CommonResult<List<InspectSchemeItemRespVO>> getInspectSchemeItemList(@RequestParam("ids") Collection<Long> ids) {
        List<InspectSchemeItemDO> list = inspectSchemeItemService.getInspectSchemeItemList(ids);
        return success(InspectSchemeItemConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得检验方案产品关联分页")
    @PreAuthorize("@ss.hasPermission('qms:inspect-scheme-item:query')")
    public CommonResult<PageResult<InspectSchemeItemRespVO>> getInspectSchemeItemPage(@Valid InspectSchemeItemPageReqVO pageVO) {
        PageResult<InspectSchemeItemDO> pageResult = inspectSchemeItemService.getInspectSchemeItemPage(pageVO);
        PageResult<InspectSchemeItemRespVO> resp = InspectSchemeItemConvert.INSTANCE.convertPage(pageResult);
        resp.getList().forEach(obj -> {
            ItemRespDTO item = itemApi.getItem(obj.getItemId());
            if (ObjectUtil.isNotEmpty(item)) {
                obj.setItemName(item.getName());
                obj.setItemCode(item.getCode());
                obj.setItemSpec(item.getSpec());
                obj.setDrawCode(item.getDrawCode());
            }
        });
        return success(resp);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出检验方案产品关联 Excel")
    @PreAuthorize("@ss.hasPermission('qms:inspect-scheme-item:export')")
    @OperateLog(type = EXPORT)
    public void exportInspectSchemeItemExcel(@Valid InspectSchemeItemExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<InspectSchemeItemDO> list = inspectSchemeItemService.getInspectSchemeItemList(exportReqVO);
        // 导出 Excel
        List<InspectSchemeItemExcelVO> datas = InspectSchemeItemConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "检验方案产品关联.xls", "数据", InspectSchemeItemExcelVO.class, datas);
    }

}
